//
//  comBlueboxmoonPhoneMACLookupFormatter.m
//  MACLookup
//
//  Created by Daniel Hazelbaker on 4/5/09.
//  Copyright 2009 Blue Box Moon. All rights reserved.
//

#import "comBlueboxmoonPhoneMACLookupFormatter.h"


@implementation comBlueboxmoonPhoneMACLookupFormatter


- (BOOL)getObjectValue:(id *)obj forString:(NSString *)string errorDescription:(NSString  **)error
{
    *obj = [NSString stringWithString:(string != nil ? string : @"")];

    return YES;
}


- (NSString *)stringForObjectValue:(id)anObject
{
    return [NSString stringWithString:(anObject != nil ? anObject : @"")];
}


//
// Force the string to conform to a mac address format.
//
- (BOOL)isPartialStringValid:(NSString *)partialString newEditingString:(NSString **)newString errorDescription:(NSString **)error
{
    NSMutableString *string = [NSMutableString string];
    char	    ch;
    int		    i;


    for (i = 0; i < [partialString length] && [string length] < 17; i++) {
	ch = [partialString characterAtIndex:i];
	if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') || ch == ':') {
	    if ((([string length] + 1) % 3) == 0) {
		[string appendString:@":"];
		if (ch == ':')
		    continue;
	    }
	    [string appendFormat:@"%c", ch];
	}
    }

    if ([string isEqual:partialString] == NO) {
	*newString = string;
	return NO;
    }

    return YES;
}


@end
